package com.ht.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ht.shortlink.project.dao.entity.LinkOsStatsDO;
import com.ht.shortlink.project.dto.req.ShortLinkStatsGroupReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

/**
 * 操作系统统计访问持久层
 */
public interface LinkOsStatsMapper extends BaseMapper<LinkOsStatsDO> {
    /**
     *  添加或更新操作系统监控数据
     */
    @Insert("INSERT INTO t_link_os_stats(gid, full_short_url, date, cnt, os, create_time, update_time, del_flag) " +
            "VALUES (#{linkOsStatsDO.gid}, #{linkOsStatsDO.fullShortUrl}, #{linkOsStatsDO.date}, #{linkOsStatsDO.cnt}, #{linkOsStatsDO.os}, NOW(), NOW(), 0) " +
            "ON DUPLICATE KEY " +
            "UPDATE cnt = cnt +  #{linkOsStatsDO.cnt};")
    void shortLinkOsStats(@Param("linkOsStatsDO") LinkOsStatsDO linkOsStatsDO);

    /**
     *  根据短链接查询操作系统监控数据
     */
    @Select("SELECT os,SUM(cnt) AS cnt FROM t_link_os_stats " +
            "WHERE full_short_url=#{param.fullShortUrl} AND gid=#{param.gid} AND date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY full_short_url,gid,os;")
    List<HashMap<String, Object>> listStatsOsByLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     *  根据分组查询操作系统监控数据
     */
    @Select("SELECT os,SUM(cnt) AS cnt FROM t_link_os_stats " +
            "WHERE gid=#{param.gid} AND date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY gid,os;")
    List<HashMap<String, Object>> listStatsOsByGroup(@Param("param") ShortLinkStatsGroupReqDTO requestParam);
}
